package monitormercado.apresentacao.comum;

public class ThreadAtualizacaoTela extends Thread {
	
	private TelaAtualizavel tela = null;
	private int sleepTime = 0;
	
	public ThreadAtualizacaoTela(TelaAtualizavel tela, int sleepTime) {
		this.tela = tela;
		this.sleepTime = sleepTime;
		this.setPriority(Thread.MAX_PRIORITY);
	}
	
	public ThreadAtualizacaoTela(TelaAtualizavel tela) {
		this(tela, 10);
	}
	
	public void run() {
		while (!tela.getShell().isDisposed() && tela.necessitaAtualizacao()) {
			tela.getDisplay().asyncExec(new Runnable() {
				public void run() {
					tela.tratarAtualizacao();
				}
			});
			try {
				Thread.sleep(sleepTime);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}	
		}

		tela.getDisplay().syncExec(new Runnable() {
				public void run() {
					tela.tratarFimExecucaoComando();
				}
		});
		
	}	
}
